# coding:utf8
import codecs
import pickle
from urllib import quote
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from parse import parse_query_car,parse_car_detail
from request_cjbx.func import get_request, get_new_session, post_request, get_request
from dbmap import sql_add_car2,del_car
from log import Logger
import settings as se
import sys
reload(sys)
log=Logger()
import traceback
sys.setdefaultencoding('utf-8')
from request_cjbx.login import get_cjbx_session
headers=se.headers
session=get_cjbx_session()
#查询车牌号
def query_car(car_num):
    url = "http://32.0.192.232:88/sinoiais/showall/query.do?dimensionSelect=03"

    car_num = quote(car_num.encode("GB2312"))
    # print car_num

    data = "queryLicensetype=&queryCredentialcode=&queryChecked=&policyNo=&vin=&licensetype=02&carmark={0}&credentialcode=01&credentialno=&cacciid=&taccidt=&dname=&lastNo=&CheckboxGroup1=02&CheckboxGroup1=03&CheckboxGroup1=04&CheckboxGroup1=08&CheckboxGroup1=05&CheckboxGroup1=07&CheckboxGroup1=09".format(
        car_num)
    headers['Content-Type'] = "application/x-www-form-urlencoded"
    r = session.post(url=url, data=data, headers=headers)

    return r.text


# 查看详情
def query_detail(PLATE_NUMBER):

    url = "http://32.0.192.232:88/sinoiais/carowner/viewJGCarOwner.do?carmark="+quote(PLATE_NUMBER.encode("gb2312"))+"&vehicletype=%D0%A1%D0%CD%C6%FB%B3%B5%BA%C5%C5%C6"
    r = session.get(url, headers=headers)
    html = r.text
    data=parse_car_detail(html)
    if data!=0:
        del_car(PLATE_NUMBER)
        sql_add_car2(data)
        return data
    else:
        return 0



